package com.jinke.api.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinke.api.modules.app.mapper.UserApiMapper;
import com.jinke.api.modules.app.model.UserApi;
import com.jinke.api.modules.app.service.UserApiService;
import com.jinke.api.modules.ums.request.UserApiRequest;
import com.jinke.api.modules.ums.service.UmsAdminService;
import com.jinke.api.modules.ums.vo.SimpleUser;
import com.jinke.api.modules.ums.vo.UserApiVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author macro
 * @since 2024-01-16
 */
@Service
public class UserApiServiceImpl extends ServiceImpl<UserApiMapper, UserApi> implements UserApiService {

    /**
     * 校验API密钥
     * @param secret
     * @return
     */
    @Override
  //  @Cacheable(value = "api:secret", key = "#secret")
    public Integer check(String secret) {
        UserApi one = getOne(new LambdaQueryWrapper<UserApi>()
                .eq(UserApi::getApiKey, secret)
                .eq(UserApi::getStatus, 1)
                .eq(UserApi::getBanned, 0)
        );
        if(one != null){
            return one.getUserId();
        }
        return 0;
    }
    @Resource
    UmsAdminService umsAdminService;

    @Override
    public Page<UserApiVO> list(UserApiRequest request) {
        List<Integer> searchIds = new ArrayList<>();
        if (StringUtils.isNotEmpty(request.getUsername())) {
            searchIds = umsAdminService.searchIds(request.getUsername());
        }

        Page<UserApi> page = new Page<>(request.getPageNum(), request.getPageSize());
        QueryWrapper<UserApi> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(!searchIds.isEmpty(), UserApi::getUserId, searchIds)
                .eq(request.getStatus() != null, UserApi::getStatus, request.getStatus())
                .eq(request.getBanned() != null, UserApi::getBanned, request.getBanned())
                .orderByDesc(UserApi::getId);

        Page<UserApi> pageResult = page(page, wrapper);
        // 获取列表中的用户ID
        List<Integer> userIds = pageResult.getRecords().stream().map(UserApi::getUserId).distinct().collect(Collectors.toList());
        // 关联用户数据
        Map<Integer, SimpleUser> usersMap = umsAdminService.getMaps(userIds);

        List<UserApiVO> vos = new ArrayList<>();
        pageResult.getRecords().forEach(x -> {
            UserApiVO vo = new UserApiVO();
            BeanUtils.copyProperties(x, vo);
            vo.setUser(usersMap.getOrDefault(x.getUserId(), null));
            vos.add(vo);
        });

        Page<UserApiVO> finalResults = new Page<>();
        finalResults
                .setRecords(vos)
                .setSize(pageResult.getSize())
                .setTotal(pageResult.getTotal())
                .setCurrent(pageResult.getCurrent());

        return finalResults;
    }
}
